<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传</title>
</head>
<body>
<h2>上传文件</h2>
<form id="uploadForm" enctype="multipart/form-data">
    <input type="file" name="file" id="fileInput" accept=".pdf,.jpg,.jpeg,.png,.doc,.docx,.bmp,.tif,.tiff,.gif">
    <button type="button" onclick="uploadFile()">上传</button>
    <button type="button" onclick="search()">匹配</button>
</form>
<!-- 添加用于显示简历文件的元素 -->
<div id="resumeContent"></div>

<script>
    function uploadFile() {
        var fileInput = document.getElementById('fileInput');
        var userEmail = sessionStorage.getItem('userEmail');

        var formData = new FormData();
        formData.append('file', fileInput.files[0]);
        formData.append('user_email', userEmail);

        fetch('http://127.0.0.1:8000/upload', {
            method: 'POST',
            body: formData,
        })
            .then(response => response.text())
            .then(data => {
                alert(data);
                // 上传成功后调用获取简历文件的方法
                getUserResume(userEmail);
            })
            .catch(error => {
                console.error('Error:', error);
            });
    }

    function search() {
        var userEmail = sessionStorage.getItem('userEmail');

        fetch('http://127.0.0.1:8000/search_resume', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: 'user_email=' + userEmail,
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                console.log(data);
                let resultString = '';
                data.result.forEach(obj => {
                    resultString += `ID: ${obj.ID}, 公司名称: ${obj.公司名称}, 岗位: ${obj.岗位}, salary: ${obj.salary}, education: ${obj.education}, description: ${obj.description}, link: ${obj.link}\n`;
                });
                alert(`search_Result:\n${resultString}`);
            })
            .catch(error => {
                console.error('Error:', error);
            });
    }

    // 获取用户简历文件的方法
    function getUserResume(userEmail) {
        fetch('http://127.0.0.1:8000/draw_file?user_email=' + userEmail)
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.blob(); // 返回二进制数据
            })
            .then(blob => {
                var url = window.URL.createObjectURL(blob); // 创建对象URL
                var a = document.createElement('a'); // 创建 <a> 元素
                a.href = url; // 设置下载链接
                a.download = 'resume.pdf'; // 设置下载文件的名称
                a.click(); // 模拟点击下载
                window.URL.revokeObjectURL(url); // 释放对象URL
            })
            .catch(error => {
                console.error('Error:', error);
            });
    }

</script>
</body>
</html>
